﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TruongVu.Data;

namespace TruongVu.Business
{
    public class DocumentModel
    {
        private int _ID;

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        private string _Title;

        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        private string _Content;

        public string Content
        {
            get { return _Content; }
            set { _Content = value; }
        }
        private string _ImagePath;

        public string ImagePath
        {
            get { return _ImagePath; }
            set { _ImagePath = value; }
        }

        public List<DocumentModel> GetFirst5()
        {
            List<DocumentModel> result = new List<DocumentModel>(); ;
            List<Document> documents = (from d in Singleton.DataContext.Documents
                                        orderby d.ID descending
                                        select d).Take(5).ToList();
            DocumentModel documentModel;
            foreach (Document document in documents)
            {
                documentModel = new DocumentModel();
                documentModel.ID = document.ID;
                documentModel.Title = document.Tittle;
                documentModel.Content = document.Content.TrimStart("~/".ToCharArray());

                if (document.Content.ToLower().EndsWith(".pdf"))
                {
                    documentModel.ImagePath = "~/Images/Mimetypes-application-pdf-icon.png";
                }
                else if (document.Content.ToLower().EndsWith(".doc")
                    || document.Content.ToLower().EndsWith(".docx"))
                {
                    documentModel.ImagePath = "~/Images/Word-icon.png";
                }
                else if (document.Content.ToLower().EndsWith(".xls")
                    || document.Content.ToLower().EndsWith(".xlsx"))
                {
                    documentModel.ImagePath = "~/Images/Excel-icon.png";
                }
                else
                {
                    documentModel.ImagePath = "~/Images/Blank-icon.png";
                }

                result.Add(documentModel);
            }

            return result;
        }

        public List<Document> GetAll()
        {
            return (from d in Singleton.DataContext.Documents
                    select d).ToList();
        }

        public Document Get(int id)
        {
            return (from d in Singleton.DataContext.Documents
                    where d.ID == id
                    select d).SingleOrDefault();
        }

        public void Insert(Document document)
        {
            Singleton.DataContext.Documents.InsertOnSubmit(document);
            Singleton.DataContext.SubmitChanges();
        }

        public void Update(Document document)
        {
            Document updatedDocument = (from d in Singleton.DataContext.Documents
                                        where d.ID == document.ID
                                        select d).SingleOrDefault();
            if (updatedDocument != null)
            {
                updatedDocument.Tittle = document.Tittle;
                updatedDocument.Content = document.Content;
                Singleton.DataContext.SubmitChanges();
            }
        }

        public void Delete(int id)
        {
            Document document = (from d in Singleton.DataContext.Documents
                                 where d.ID == id
                                 select d).SingleOrDefault();
            if (document != null)
            {
                Singleton.DataContext.Documents.DeleteOnSubmit(document);
                Singleton.DataContext.SubmitChanges();
            }
        }
    }
}
